from rest_framework import serializers
from .models import CustomUser


class UserSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True)

    class Meta:
        model = CustomUser
        fields = '__all__'
        extra_kwargs = {
            'password': {'write_only': True},
            'avatar': {'required': False, 'allow_null': True, 'default': 'default_avatar.jpg'}
        }
        read_only_fields = ('id', 'is_superuser', 'is_staff')  # 只读字段

    def create(self, validated_data):
        user = CustomUser.objects.create_user(**validated_data)
        return user
